/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.ArrayList;
import model.Danhmuc;
import model.Loaidanhmuc;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 *
 * @author HoangLiem
 */
public class DanhMucDAO {
    public static ArrayList<Danhmuc> getAll() {
        ArrayList<Danhmuc> list = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction trans = session.getTransaction();
            trans.begin();
            String hql = "from Danhmuc";
            Query query = session.createQuery(hql);
            list = (ArrayList<Danhmuc>)query.list();
            trans.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }
    public static ArrayList<Danhmuc> getAllByCategoryId(Loaidanhmuc ldm) {
        ArrayList<Danhmuc> list = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction trans = session.getTransaction();
            trans.begin();
            String hql = "from Danhmuc dm where dm.loaidanhmuc=:ldm";
            Query query = session.createQuery(hql);
            query.setEntity("ldm", ldm);
            list = (ArrayList<Danhmuc>)query.list();
            trans.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }
    public static Danhmuc getCategoryById(String maDanhMuc) {
        Danhmuc dm = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction trans = session.getTransaction();
            trans.begin();
            String hql = "from Danhmuc dm where dm.maDanhMuc=:maDanhMuc";
            Query query = session.createQuery(hql);
            query.setString("maDanhMuc", maDanhMuc);
            dm = (Danhmuc)query.uniqueResult();
            trans.commit();
        } catch (Exception ex) {
            dm = null;
            ex.printStackTrace();
        }
        return dm;
    }
}
